package com.ruoyi.web.controller.video;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.video.domain.BusVideoInfo;
import com.ruoyi.video.domain.vo.VideoUploadParam;
import com.ruoyi.video.service.IVideoInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 视频 信息操作处理
 * 
 * @author ruoyi
 */
@Api("视频信息管理")
@RestController
@RequestMapping("/bus/video")
public class BusVideoController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BusVideoController.class);

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IVideoInfoService videoInfoService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 获取视频列表
     */
    @ApiOperation("获取视频列表")
    @PreAuthorize("@ss.hasPermi('bus:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusVideoInfo video) {
        startPage();
        List<SysDictData> videoTagDictList = dictTypeService.selectDictDataByType("video_tag");
        List<SysDictData> videoTypeDictList = dictTypeService.selectDictDataByType("video_type");
        List<BusVideoInfo> videoInfoList = videoInfoService.selectVideoList(video);
        videoInfoService.formatVideoList(videoInfoList, videoTagDictList, videoTypeDictList);
        return getDataTable(videoInfoList);
    }

    /**
     * 根据视频编号获取详细信息
     */
    @ApiOperation("获取视频详情")
    @PreAuthorize("@ss.hasPermi('bus:video:query')")
    @GetMapping(value = "/{videoId}")
    public AjaxResult getInfo(@PathVariable Long videoId) {
        return AjaxResult.success(videoInfoService.selectVideoById(videoId));
    }

    /**
     * 新增视频
     */
    @ApiOperation("上传视频/封面图/文件信息")
    @PreAuthorize("@ss.hasPermi('bus:video:import')")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping("/importFile")
    public AjaxResult importFile(MultipartFile file) {
        try {
            String filePath = RuoYiConfig.getUploadPath();  // 上传文件路径
            // 上传视频
            String fileName = FileUploadUtils.upload(filePath, file);
            String fileUrl = serverConfig.getUrl() + fileName;
            return AjaxResult.success(fileUrl);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增视频
     */
    @ApiOperation("保存视频信息")
    @PreAuthorize("@ss.hasPermi('bus:video:add')")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody VideoUploadParam param) {
        BusVideoInfo videoInfo = new BusVideoInfo();
        try {
            videoInfo.setVideoPath(param.getVideoPath());
            videoInfo.setVideoName(param.getVideoName());
            videoInfo.setVideoType(param.getVideoType());
            videoInfo.setVideoTag(param.getVideoTag());
            videoInfo.setRemark(param.getVideoRemark());
            videoInfo.setVideoCover(param.getVideoCover());
            videoInfo.setCreateBy(SecurityUtils.getUsername());
            videoInfo.setUpdateBy(SecurityUtils.getUsername());
            videoInfoService.insertVideo(videoInfo);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除视频
     */
    @ApiOperation("删除视频信息")
    @PreAuthorize("@ss.hasPermi('bus:video:remove')")
    @Log(title = "视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{videoIds}")
    public AjaxResult remove(@PathVariable Long[] videoIds) {
        return toAjax(videoInfoService.deleteVideoByIds(videoIds));
    }
}
